package com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.ApplyPermissionActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.SettingActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseLoanFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden.DataSafetyFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden.LoanFailFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden.LoanMainFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden.LoanWaitFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden.PersonalCenterFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden.RepayFragment_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.fragment_wooden.SignContractFragment;
import com.test.nnooolwdeaer.bean_wooden.StaticVariate_wooden;
import com.test.nnooolwdeaer.bean_wooden.config_info_wooden.AppShowInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.process_wooden.ProcessInfo_wooden;
import com.test.nnooolwdeaer.bean_wooden.star_dialog.ActiveWindowConfig_wooden;
import com.test.nnooolwdeaer.bean_wooden.star_dialog.NormalWindowConfig_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.dialog_wooden.InitOfflinePayChannelDialog_wooden;
import com.test.nnooolwdeaer.dialog_wooden.NoticeDialog_wooden;
import com.test.nnooolwdeaer.dialog_wooden.star_dialog.StarActiveDialog_wooden;
import com.test.nnooolwdeaer.dialog_wooden.star_dialog.StarNormalDialog_wooden;
import com.test.nnooolwdeaer.util_wooden.PermissionUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.analytics_wooden.util_wooden.AnalyticsUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.event.TrackManager_wooden;
import com.test.nnooolwdeaer.util_wooden.helper_wooden.UserInfoHelper_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseLoanActivity_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.BindBankAccountActivity_Cote_wooden;
import com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden.InfoUploadActivity_wooden;

import java.util.Objects;


public class LoanMainActivity_wooden extends BaseLoanActivity_wooden {

    private ViewGroup homeLayout_wooden, dataSafetyLayout_wooden, personalCenterLayout_wooden;
    private ImageView homeIv_wooden, dataSafetyIv_wooden, personalCenterIv_wooden;
    private TextView homeTv_wooden, dataSafetyTv_wooden, personalCenterTv_wooden;

    private int fragmentShow_wooden = 1;

    public BaseLoanFragment_wooden homeFragment_wooden;
    private DataSafetyFragment_wooden dataSafetyFragment_wooden;
    private PersonalCenterFragment_wooden personalCenterFragment_wooden;

    public static boolean reviewNoticeFlag = true;
    public static boolean loaningNoticeFlag = true;
    public static boolean repayNoticeFlag = true;

    public static boolean launchSettingFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_main_wooden);
        TrackManager_wooden.INSTANCE.deleteEvents();
        init_wooden();
    }

    private void init_wooden() {
        homeLayout_wooden = findViewById(R.id.activity_loan_main_layout_home);
        homeIv_wooden = findViewById(R.id.activity_loan_main_iv_home);
        homeTv_wooden = findViewById(R.id.activity_loan_main_tv_home);
        dataSafetyLayout_wooden = findViewById(R.id.activity_loan_main_layout_data_safety);
        dataSafetyIv_wooden = findViewById(R.id.activity_loan_main_iv_data_safety);
        dataSafetyTv_wooden = findViewById(R.id.activity_loan_main_tv_data_safety);
        personalCenterLayout_wooden = findViewById(R.id.activity_loan_main_layout_personal_center);
        personalCenterIv_wooden = findViewById(R.id.activity_loan_main_iv_personal_center);
        personalCenterTv_wooden = findViewById(R.id.activity_loan_main_tv_personal_center);

        dataSafetyFragment_wooden = new DataSafetyFragment_wooden();
        personalCenterFragment_wooden = new PersonalCenterFragment_wooden(this);

        homeLayout_wooden.setOnClickListener(v -> changeToHome_wooden());
        dataSafetyLayout_wooden.setOnClickListener(v -> changeToDataSafety_wooden());
        personalCenterLayout_wooden.setOnClickListener(v -> changeToPersonalCenter_wooden());

    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePage_wooden();
    }

    private void updatePage_wooden() {
        try {

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

            if (homeFragment_wooden != null) fragmentTransaction.remove(homeFragment_wooden);
            if (dataSafetyFragment_wooden != null)
                fragmentTransaction.remove(dataSafetyFragment_wooden);
            if (personalCenterFragment_wooden != null)
                fragmentTransaction.remove(personalCenterFragment_wooden);

            if (ProcessInfo_wooden.getInstance_wooden().isOrderPendingRepayment_wooden() || ProcessInfo_wooden.getInstance_wooden().isOrderOverdueRepayment_wooden()) {//订单状态-待还款
                homeFragment_wooden = new RepayFragment_wooden();
            } else if (ProcessInfo_wooden.getInstance_wooden().isOrderLoaning_wooden() || ProcessInfo_wooden.getInstance_wooden().isLoanReviewing_wooden()) {//订单状态-放款中/审核中
                if (ProcessInfo_wooden.getInstance_wooden().isNeedSign())
                    homeFragment_wooden = new SignContractFragment();
                else homeFragment_wooden = new LoanWaitFragment_wooden();
            } else if (ProcessInfo_wooden.getInstance_wooden().isLoanRejcted_wooden()) {//订单状态-被拒绝
                homeFragment_wooden = new LoanFailFragment_wooden();
                launchSettingFlag = false;
                HttpManager_wooden.getInstance_wooden().fetchDropConfig();
            } else {
                homeFragment_wooden = new LoanMainFragment_wooden();
            }

            fragmentTransaction.add(R.id.activity_loan_main_layout_fragment, Objects.requireNonNull(homeFragment_wooden))
                    .add(R.id.activity_loan_main_layout_fragment, dataSafetyFragment_wooden)
                    .add(R.id.activity_loan_main_layout_fragment, personalCenterFragment_wooden);

            switch (fragmentShow_wooden) {
                case 2 ->
                        fragmentTransaction.show(dataSafetyFragment_wooden).hide(homeFragment_wooden).hide(personalCenterFragment_wooden);
                case 3 ->
                        fragmentTransaction.show(personalCenterFragment_wooden).hide(homeFragment_wooden).hide(dataSafetyFragment_wooden);
                default ->
                        fragmentTransaction.show(homeFragment_wooden).hide(dataSafetyFragment_wooden).hide(personalCenterFragment_wooden);
            }

            fragmentTransaction.commitAllowingStateLoss();

        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    private void changeToHome_wooden() {
        try {
            homeIv_wooden.setImageResource(R.drawable.icon_tab_home_true);
            dataSafetyIv_wooden.setImageResource(R.drawable.icon_tab_data_safety_false);
            personalCenterIv_wooden.setImageResource(R.drawable.icon_tab_personal_center_false);

            homeTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.blue_wooden));
            dataSafetyTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.gray_dd));
            personalCenterTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.gray_dd));

            getSupportFragmentManager()
                    .beginTransaction().show(homeFragment_wooden).hide(dataSafetyFragment_wooden).hide(personalCenterFragment_wooden)
                    .commitAllowingStateLoss();

            fragmentShow_wooden = 1;
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    private void changeToDataSafety_wooden() {
        try {
            homeIv_wooden.setImageResource(R.drawable.icon_tab_home_false);
            dataSafetyIv_wooden.setImageResource(R.drawable.icon_tab_data_safety_true);
            personalCenterIv_wooden.setImageResource(R.drawable.icon_tab_personal_center_false);

            homeTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.gray_dd));
            dataSafetyTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.blue_wooden));
            personalCenterTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.gray_dd));

            getSupportFragmentManager()
                    .beginTransaction().show(dataSafetyFragment_wooden).hide(homeFragment_wooden).hide(personalCenterFragment_wooden)
                    .commitAllowingStateLoss();

            fragmentShow_wooden = 2;
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    private void changeToPersonalCenter_wooden() {
        try {
            homeIv_wooden.setImageResource(R.drawable.icon_tab_home_false);
            dataSafetyIv_wooden.setImageResource(R.drawable.icon_tab_data_safety_false);
            personalCenterIv_wooden.setImageResource(R.drawable.icon_tab_personal_center_true);

            homeTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.gray_dd));
            dataSafetyTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.gray_dd));
            personalCenterTv_wooden.setTextColor(ContextCompat.getColor(this, R.color.blue_wooden));

            getSupportFragmentManager()
                    .beginTransaction().show(personalCenterFragment_wooden).hide(homeFragment_wooden).hide(dataSafetyFragment_wooden)
                    .commitAllowingStateLoss();

            fragmentShow_wooden = 3;
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    //配置信息返回响应
    public void onAppShowInfoResponse_wooden() {
        if (personalCenterFragment_wooden != null)
            personalCenterFragment_wooden.updateCustomerService_wooden();
    }

    //额度列表返回响应
    public void onLoanLimitInfoResponse_wooden() {
        if (homeFragment_wooden != null && homeFragment_wooden instanceof LoanMainFragment_wooden)
            ((LoanMainFragment_wooden) homeFragment_wooden).updateLoanAmount_wooden();
    }

    @Override
    public void onUserProcessResponse_wooden() {
        //订单状态-待还款
        if (ProcessInfo_wooden.getInstance_wooden().isOrderPendingRepayment_wooden() || ProcessInfo_wooden.getInstance_wooden().isOrderOverdueRepayment_wooden()) {
            if (homeFragment_wooden instanceof RepayFragment_wooden) {
                HttpManager_wooden.getInstance_wooden().getRepayDetail_wooden();
            } else updatePage_wooden();
            return;
        }
        //订单状态-审核中/放款中
        if (ProcessInfo_wooden.getInstance_wooden().isLoanReviewing_wooden() || ProcessInfo_wooden.getInstance_wooden().isOrderLoaning_wooden()) {
            updatePage_wooden();
            return;
        }
        //订单状态-被拒绝
        if (ProcessInfo_wooden.getInstance_wooden().isLoanRejcted_wooden()) {
            if (!(homeFragment_wooden instanceof LoanFailFragment_wooden)) updatePage_wooden();
            return;
        }
        //其他订单状态
        if (homeFragment_wooden instanceof LoanMainFragment_wooden) {
            if (((LoanMainFragment_wooden) homeFragment_wooden).isRefresh_wooden) {
                HttpManager_wooden.getInstance_wooden().queryLoanLimit_wooden();
                ((LoanMainFragment_wooden) homeFragment_wooden).isRefresh_wooden = false;
            } else startLoanProcess_wooden();
        } else {
            updatePage_wooden();
        }
    }

    //借款流程
    private void startLoanProcess_wooden() {

        if (!PermissionUtil_wooden.checkLaunchPermissions_wooden(this, Manifest.permission.CAMERA)) {
            startActivity_wooden(ApplyPermissionActivity_wooden.class);
            return;
        }

        try {
            if (UserInfoHelper_wooden.getInstance_wooden().didLogin_wooden()) {
                AnalyticsUtil_wooden.INSTANCE.saveDeviceS2SInfo();
            }
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        //科特的绑卡环节
        if (StaticVariate_wooden.Select_Country_wooden.equalsIgnoreCase(Constant_wooden.Country_Cote_wooden) &&
                ProcessInfo_wooden.getInstance_wooden().getStepName_wooden(0).equalsIgnoreCase("account_info")) {
            startActivity_wooden(BindBankAccountActivity_Cote_wooden.class);
        } else if (ProcessInfo_wooden.getInstance_wooden().getStepsSize_wooden() > 0) {
            startActivity_wooden(InfoUploadActivity_wooden.class);
        } else {
            //如果不是处理还款流程、状态流程、认证流程，直接去借款确认页
            startActivity_wooden(LoanApplyActivity_wooden.class);
            HttpManager_wooden.getInstance_wooden().requestLoanApplyDetail_wooden();
        }

    }

    public void onLoanApplyDetailResponse_wooden() {

        //订单状态-放款中/审核中
        if (ProcessInfo_wooden.getInstance_wooden().isOrderLoaning_wooden() || ProcessInfo_wooden.getInstance_wooden().isLoanReviewing_wooden()) {
            if ((homeFragment_wooden instanceof LoanWaitFragment_wooden))
                ((LoanWaitFragment_wooden) homeFragment_wooden).onLoanApplyDetailResponse_wooden();
        } else startActivity_wooden(LoanApplyActivity_wooden.class);
    }

    public void onRepayDetailResponse_wooden() {
        if (homeFragment_wooden != null && homeFragment_wooden instanceof RepayFragment_wooden) {
            ((RepayFragment_wooden) homeFragment_wooden).updateRepayDetail_wooden();

            String notice = AppShowInfo_wooden.getInstance().repayNotice();
            if (repayNoticeFlag && !TextUtils.isEmpty(notice)) {
                repayNoticeFlag = false;
                new NoticeDialog_wooden(this, notice).show();
            }
        }
    }

    public void onOrderRepayH5Response_wooden(String url) {
        if (homeFragment_wooden != null && homeFragment_wooden instanceof RepayFragment_wooden) {
            Uri uri = Uri.parse(StringUtil_wooden.getSafeString_wooden(url));
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }

    //获取bankAccountList返回(科特)
    public void onFetchBankAccountListResponse_wooden() {
        //更新科特放款卡列表
        if (homeFragment_wooden instanceof LoanWaitFragment_wooden)
            ((LoanWaitFragment_wooden) homeFragment_wooden).updateWalletAccounts_Cote_wooden();
    }

    //获取线下还款通道响应-尼日
    public void onInitOfflinePayChannelResponse_wooden() {
        InitOfflinePayChannelDialog_wooden.showDialog(this, dialogInterface -> HttpManager_wooden.getInstance_wooden().getRepayDetail_wooden());
    }


    public void onStarWindowConfigResponse() {
        if (homeFragment_wooden != null) {
            //评分弹窗显示流程

            //1.活动弹窗
            if (ActiveWindowConfig_wooden.getInstance_wooden().isSwitch_wooden() &&
                    UserInfoHelper_wooden.getInstance_wooden().getActivedialogCount() < ActiveWindowConfig_wooden.getInstance_wooden().getNum_wooden()) {
                new StarActiveDialog_wooden(this).show();
                return;
            }

            //2.评价弹窗
            if (NormalWindowConfig_wooden.getInstance_wooden().isSwitch_wooden() &&
                    UserInfoHelper_wooden.getInstance_wooden().getNormaldialogCount() < NormalWindowConfig_wooden.getInstance_wooden().getNum_wooden()) {
                new StarNormalDialog_wooden(this).show();
                return;
            }


            if (ProcessInfo_wooden.getInstance_wooden().isOrderLoaning_wooden() && loaningNoticeFlag) {//放款中

                String notice = AppShowInfo_wooden.getInstance().loaningNotice();

                if (!TextUtils.isEmpty(notice)) {
                    loaningNoticeFlag = false;
                    new NoticeDialog_wooden(this, notice).show();
                }

            } else if (ProcessInfo_wooden.getInstance_wooden().isLoanReviewing_wooden() && reviewNoticeFlag) {//审核中

                String notice = AppShowInfo_wooden.getInstance().reviewNotice();

                if (!TextUtils.isEmpty(notice)) {
                    reviewNoticeFlag = false;
                    new NoticeDialog_wooden(this, notice).show();
                }

            }

        }
    }

    public void onDropConfigResponse(boolean showDropBtn) {

        if (launchSettingFlag) {
            startActivity(new Intent(this, SettingActivity_wooden.class).putExtra("showDropBtn", showDropBtn));
        } else if (homeFragment_wooden != null && homeFragment_wooden instanceof LoanFailFragment_wooden loanFailFragment) {
            loanFailFragment.onDropConfigResponse(showDropBtn);
        }

        launchSettingFlag = true;
    }


}